package com.eu.ebpm;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 从spring上下文获取bean
 * @author 王晓华
 * @date 2013-06-16
 * @version 1.0
 *
 */
public class SpringContext implements ApplicationContextAware {
	
	/**
	 * 到spring ApplicationContext的引用
	 */
	private static ApplicationContext applicationContext;

	/***
	 * 
	 * 设置spring上下文
	 * @param applicationContext spring上下文
	 * 
	 */
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		SpringContext.applicationContext = applicationContext;
	}

	/***
	 * @param name bean 名称
	 * @return 引用对象
	 * @throws BeansException 异常
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) throws BeansException {
		return (T) SpringContext.applicationContext.getBean(name);
	}
	
	public static <T> T getBean(Class<T> clazz) throws BeansException {
		return SpringContext.applicationContext.getBean(clazz);
	}

}
